package com.cms.controllers.admin;

import com.cms.common.aop.permission.PermissionCheck;
import com.cms.common.model.entity.Article;
import com.cms.common.model.entity.ArticleCategory;
import com.cms.common.model.entity.Translate;
import com.cms.common.model.services.ArticleCategoryService;
import com.cms.common.model.services.ArticleService;
import com.cms.common.model.services.TranslateService;
import com.cms.common.model.vo.RequestData;
import com.cms.common.model.vo.ResponseResult;
import com.cms.controllers.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin/article")
public class ArticleController extends BaseController {

    @Autowired
    ArticleService articleService;
    @Autowired
    ArticleCategoryService articleCategoryService;

    @Autowired
    TranslateService translateService;


    @GetMapping("")
    @PermissionCheck(name = "文章列表", typeName = "文章管理")
    public String index() {
        return "admin/article/index";
    }

    @GetMapping("/list")
    @ResponseBody
    @PermissionCheck(name = "文章列表数据", typeName = "文章管理")
    public ResponseResult articles(@ModelAttribute RequestData requestData) {
        Page<Article> articles = articleService.getAllByPage(requestData);
        return ResponseResult.success(articles);
    }

    @GetMapping("/create")
    @PermissionCheck(name = "创建文章界面", typeName = "文章管理")
    public String create(Model viewModel) {
        //生成分组数据
        viewModel.addAttribute("article", new Article());
        List<ArticleCategory> articleCategories = articleCategoryService.getAll();
        viewModel.addAttribute("categories", articleCategories);
        return "admin/article/form";
    }

    @PostMapping("/store")
    @PermissionCheck(name = "创建文章", typeName = "文章管理")
    @ResponseBody
    public ResponseResult store(@RequestParam("category_id") Long category_id, @RequestParam("title") String title, @RequestParam("content") String content) {

        ArticleCategory articleCategory = articleCategoryService.getById(category_id);
        if (articleCategory == null) {
            ResponseResult.fail("文章分类不存在");
        }

        Article article = new Article();

        //设置标题
        Translate titleTranslate = new Translate();
        titleTranslate.setContent(title);
        List<Translate> titleTranslates = new ArrayList<>();
        titleTranslates.add(titleTranslate);
        titleTranslates = translateService.addTranslates(titleTranslates);
        article.setTitleTranslates(titleTranslates);

        //设置内容
        Translate contentTranslate = new Translate();
        contentTranslate.setContent(content);
        List<Translate> contentTranslates = new ArrayList<>();
        contentTranslates.add(contentTranslate);
        contentTranslates = translateService.addTranslates(contentTranslates);
        article.setContentTranslates(contentTranslates);

        article.setCategory(articleCategory);
        article = articleService.addArticle(article);

        return ResponseResult.success(article);
    }

    @GetMapping("/edit/{id}")
    @PermissionCheck(name = "修改文章界面", typeName = "文章管理")
    public String edit(@PathVariable Long id, Model viewModel) {
        Article article = articleService.getById(id);
        viewModel.addAttribute("article", article);
        List<ArticleCategory> articleCategories = articleCategoryService.getAll();
        viewModel.addAttribute("categories", articleCategories);
        String htmlContent = "<p style='color:red;'>This is a <b>test</b>.</p>";
        viewModel.addAttribute("test", htmlContent);
        return "admin/article/form";
    }

    @PutMapping("/update/{id}")
    @PermissionCheck(name = "修改文章", typeName = "文章管理")
    @ResponseBody
    public ResponseResult update(@PathVariable Long id, @RequestParam("category_id") Long category_id, @RequestParam("title") String title, @RequestParam("content") String content) {

        Article article = articleService.getById(id);
        if (article == null) {
            ResponseResult.fail("文章不存在");
        }

        ArticleCategory articleCategory = articleCategoryService.getById(category_id);
        if (articleCategory == null) {
            ResponseResult.fail("文章分类不存在");
        }


        //设置标题
        translateService.deleteTranslates(article.getTitleTranslates());
        Translate titleTranslate = new Translate();
        titleTranslate.setContent(title);
        List<Translate> titleTranslates = new ArrayList<>();
        titleTranslates.add(titleTranslate);
        titleTranslates = translateService.addTranslates(titleTranslates);
        article.setTitleTranslates(titleTranslates);

        //设置内容
        translateService.deleteTranslates(article.getContentTranslates());
        Translate contentTranslate = new Translate();
        contentTranslate.setContent(content);
        List<Translate> contentTranslates = new ArrayList<>();
        contentTranslates.add(contentTranslate);
        contentTranslates = translateService.addTranslates(contentTranslates);
        article.setContentTranslates(contentTranslates);

        article.setCategory(articleCategory);

        article = articleService.addArticle(article);
        return ResponseResult.success(article);
    }

    @DeleteMapping("/destroy/{id}")
    @PermissionCheck(name = "删除文章", typeName = "文章管理")
    @ResponseBody
    public ResponseResult destroy(@PathVariable Long id) {
        Article article = articleService.getById(id);
        if (article == null) {
            ResponseResult.fail("文章不存在");
        }
        translateService.deleteTranslates(article.getTitleTranslates());
        translateService.deleteTranslates(article.getContentTranslates());
        return ResponseResult.success(article);
    }
}
